﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static DataNatTool.NatUserControl;

namespace DataNatTool
{
    public partial class NatUserControl : UserControl
    {
        private int Id;
        public NatUserControl(int id)
        {
            Id = id;
            InitializeComponent();
        }
        DataNat dataNat1 = new DataNat();
        private void NatUserControl_Load(object sender, EventArgs e)
        {
            groupBox1.Text = $"设备:{Id}";

            dungeonComboBox1.Items.Add("TcpServer");
            dungeonComboBox1.Items.Add("TcpClient");
            //dungeonComboBox1.Items.Add("COM");
            //dungeonComboBox1.SelectedIndex = 0;
            dungeonComboBox2.Items.Add("TcpServer");
            dungeonComboBox2.Items.Add("TcpClient");
            //dungeonComboBox2.Items.Add("COM");
            //dungeonComboBox2.SelectedIndex = 0;

            ReadConfig();

            dataNat1.OnOnlineColorSent += DataNat_OnOnlineColorSent;
        }

        private void DataNat_OnOnlineColorSent(object sender, OnlineColorEventArgs e)
        {
            badge1.Value = e.OnlineCountLeft;
            badge2.Value = e.OnlineCountRight;

            if (badge1.Value > 0)
                badge1.BGColorA = Color.Green;
            else
                badge1.BGColorA = Color.Red;

            if (badge2.Value > 0)
                badge2.BGColorA = Color.Green;
            else
                badge2.BGColorA = Color.Red;
        }

        private void dungeonToggleButton1_ToggledChanged()
        {
            dataNat1.OpenLeft_ToggledChanged(dungeonComboBox1.SelectedIndex, dungeonToggleButton1.Toggled, dungeonTextBox1.Text, dungeonTextBox2.Text);
        }

        private void dungeonToggleButton2_ToggledChanged()
        {
            dataNat1.OpenRight_ToggledChanged(dungeonComboBox2.SelectedIndex, dungeonToggleButton2.Toggled, dungeonTextBox3.Text, dungeonTextBox4.Text);
        }

        //下拉选项
        private void dungeonComboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReadConfig();
        }
        //读取配置
        private void ReadConfig()
        {
            try
            {
                var index = dungeonComboBox3.SelectedIndex;

                string configJsonStr = Properties.Settings.Default.设置;
                var configClasses = JsonConvert.DeserializeObject<ConfigClass[]>(configJsonStr).ToArray();
                var configClass = configClasses.Where(it => it.Id == Id).First();
                var sonConfigClass = configClass.SonConfigs.Where(it => it.Id == index).First();

                dungeonComboBox1.SelectedIndex = sonConfigClass.LeftConfig.SelectedIndex;
                dungeonTextBox1.Text = sonConfigClass.LeftConfig.IP;
                dungeonTextBox2.Text = sonConfigClass.LeftConfig.Port;

                dungeonComboBox2.SelectedIndex = configClass.SonConfigs[index].RightConfig.SelectedIndex;
                dungeonTextBox3.Text = sonConfigClass.RightConfig.IP;
                dungeonTextBox4.Text = sonConfigClass.RightConfig.Port;
            }
            catch (Exception)
            {

            }
        }

        //写配置
        private void dungeonButtonRight1_Click(object sender, EventArgs e)
        {
            try
            {
                //配置
                var index = dungeonComboBox3.SelectedIndex;
                var configClasses = new List<ConfigClass>();

                string configJsonStr = Properties.Settings.Default.设置;
                if (configJsonStr == null)
                {
                    //初始化配置
                    for (int i = 1; i <= 8; i++)
                    {
                        var sonConfigs = new List<SonConfigClass>();
                        for (int j = 0; j < 8; j++)
                        {
                            sonConfigs.Add(new SonConfigClass()
                            {
                                Id = j,
                                LeftConfig = new SonSonConfigClass(),
                                RightConfig = new SonSonConfigClass(),
                            });
                        }

                        configClasses.Add(new ConfigClass()
                        {
                            Id = i,
                            SonConfigs = sonConfigs.ToArray(),
                        });
                    }
                }
                else
                {
                    configClasses = JsonConvert.DeserializeObject<ConfigClass[]>(configJsonStr).ToList();
                    var configClass = configClasses.Where(it => it.Id == Id).First();
                    var sonConfigClass = configClass.SonConfigs.Where(it => it.Id == index).First();

                    sonConfigClass.LeftConfig.SelectedIndex = dungeonComboBox1.SelectedIndex;
                    sonConfigClass.LeftConfig.IP = dungeonTextBox1.Text;
                    sonConfigClass.LeftConfig.Port = dungeonTextBox2.Text;

                    configClass.SonConfigs[index].RightConfig.SelectedIndex = dungeonComboBox2.SelectedIndex;
                    sonConfigClass.RightConfig.IP = dungeonTextBox3.Text;
                    sonConfigClass.RightConfig.Port = dungeonTextBox4.Text;
                }

                var res = JsonConvert.SerializeObject(configClasses);
                Properties.Settings.Default.设置 = res;
                Properties.Settings.Default.Save(); // 别忘了保存设置
            }
            catch (Exception)
            {

            }
        }
        public class ConfigClass 
        {
            public int Id { get; set; }
            public SonConfigClass[] SonConfigs { get; set; }
        }
        public class SonConfigClass
        {
            public int Id { get; set; } 
            public SonSonConfigClass LeftConfig { get; set; }
            public SonSonConfigClass RightConfig { get; set; }
        }

        public class SonSonConfigClass
        {
            public int SelectedIndex { get; set; } = 0;
            public string IP { get; set; } = "0.0.0.0";
            public string Port { get; set; } = "15000";
        }

        
    }
}
